<!-- 课程列编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改课程列':'添加课程列'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

     <el-form-item label="老师:" prop="teacherId">
         <el-select
           filterable
           clearable
           v-model="form.teacherId"
           @change="$forceUpdate()"
           size="small"
           placeholder="-请选择老师-"
           class="ele-block">
           <el-option v-for="item in teachList" :key="item.id" :label="item.name" :value="item.id"/>
         </el-select>
        </el-form-item>
       <el-form-item label="年级:" prop="gradeId">
        <el-select
          filterable
          clearable
          v-model="form.gradeId"
          @change="$forceUpdate()"
          size="small"
          placeholder="-请选择年级-"
          class="ele-block">
          <el-option v-for="item in gradeList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
       </el-form-item>

      <el-form-item label="班级:" prop="classId">
       <el-select
         filterable
         clearable
         v-model="form.classId"
         @change="$forceUpdate()"
         size="small"
         placeholder="-请选择班级-"
         class="ele-block">
         <el-option v-for="item in classList" :key="item.id" :label="item.name" :value="item.id"/>
       </el-select>
      </el-form-item>

     <el-form-item label="专业:" prop="profId">
      <el-select
        filterable
        clearable
        v-model="form.profId"
        @change="$forceUpdate()"
        size="small"
        placeholder="-请选择专业-"
        class="ele-block">
        <el-option v-for="item in profList" :key="item.id" :label="item.name" :value="item.id"/>
      </el-select>
     </el-form-item>
    <el-form-item
        label="授课日期:"
        prop="section_date">
          <el-date-picker
          type="date"
          class="ele-fluid"
          v-model="form.section_date"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          placeholder="请输入授课日期"/>
      </el-form-item>
      <el-form-item
        label="节次:"
        prop="section">
        <el-input
          :maxlength="20"
          v-model="form.section"
          placeholder="请输入节次"
          clearable/>
      </el-form-item>
      <el-form-item
        label="授课内容:"
        prop="title">
        <el-input
          :maxlength="20"
          v-model="form.title"
          placeholder="请输入授课内容"
          clearable/>
      </el-form-item>

      <el-form-item label="授课要求:">
        <el-input
          :rows="3"
          clearable
          type="textarea"
          :maxlength="200"
          v-model="form.note"
          placeholder="请输入授课要求"/>
      </el-form-item>
          </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'CourseEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              gradeId: [
          {required: true, message: '请选择年级', trigger: 'blur'}
        ],
                  teacherId: [
          {required: true, message: '请选择老师', trigger: 'blur'}
        ],
                  profId: [
          {required: true, message: '请选择专业', trigger: 'blur'}
        ],
        classId: [
          {required: true, message: '请选择班级', trigger: 'blur'}
        ],
                  section: [
          {required: true, message: '请输入结次', trigger: 'blur'}
        ],
                  section_date: [
          {required: true, message: '请选择授课日期', trigger: 'blur'}
        ],
                  title: [
          {required: true, message: '请输入授课内容', trigger: 'blur'}
        ],
                  note: [
          {required: true, message: '请输入授课要求', trigger: 'blur'}
        ],
            },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      gradeList: [],
      teachList:[],
      profList:[],
      classList:[],
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  mounted() {

    // 获取年级列表
    this.getGradeList();
    this.getProfList();
    this.getTeachList();
    this.getClassList();
  },
  methods: {
    getClassList() {
      this.$http.get('/prof/getClassList').then(res => {
        if (res.data.code === 0) {
          this.classList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    getTeachList() {
      this.$http.get('/prof/getTeachList').then(res => {
        if (res.data.code === 0) {
          this.teachList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    getProfList() {
      this.$http.get('/prof/getProfList').then(res => {
        if (res.data.code === 0) {
          this.profList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取年级列表
     */
    getGradeList() {
      this.$http.get('/grade/getGradeList').then(res => {
        if (res.data.code === 0) {
          this.gradeList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http.post('/course/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
